package com.daka.entity;

import com.daka.tools.ConfigUtil;
import com.daka.tools.DateUtil;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.Alias;

import java.text.ParseException;
import java.util.Date;

/**
 * Created by yangx
 * Date: 2016/7/25 0025
 * Time: 10:16
 */

@SuppressWarnings("unused")
@Data
@ToString
@Alias(value = "U_USER")
@Slf4j
public class User {

    /**
     * .
     */
    private Long id;

    /**
     * .注册手机号
     */
    private String mobile;

    /**
     * .第三方登录构造email数据
     */
    private String email;

    /**
     * .昵称
     */
    private String nickName;

    /**
     * .真实姓名
     */
    private String realName;

    /**
     * .密码
     */
    private String password;

    /**
     * .用户角色
     */
    private String roles;

    /**
     * .删除状态 0未删除 1已删除
     */
    private Integer delStatus;

    /**
     * .注册来源 1 安卓2苹果 3网页
     */
    private Integer platform;

    /**
     * .
     */
    private Date createTime;

    /**
     * .
     */
    private Integer locked;

    /**
     * .
     */
    private Long oldid;

    /**
     * .
     */
    private String oldMemberCode;

    /**
     * .
     */
    private String area;

    /**
     * .
     */
    private Double lat;

    /**
     * .
     */
    private Double lng;

    /**
     * .
     */
    private String client;

    public static User build(Member member) {
        User user = new User();

        if (StringUtils.isEmpty(member.getBundmobile())) {
            ConfigUtil.buildConfig();
            ConfigUtil.tempMobile = ConfigUtil.tempMobile + 1;
            user.setMobile(ConfigUtil.tempMobile.toString());
        } else {
            user.setMobile(member.getBundmobile());
        }

        user.setEmail(member.getEmail());
        user.setNickName(member.getNickname());
        user.setRealName(member.getRealname());
        user.setPassword(member.getPassword());
        user.setRoles(null);
        user.setDelStatus(member.getDelstatus());

        if (member.getResource() == 1) {
            user.setPlatform(0);
        } else {
            user.setPlatform(2);
        }

        Date date;
        try {
            date = DateUtil.str2Date(member.getRegdate(), DateUtil.DATETIME_YYYY_MM_DD_HH_MM_SS);
        } catch (Exception e) {
            date = new Date();
            log.error("===============dakaqi============== Date Format Error! User email : " + user.getEmail() );
        }

        user.setCreateTime(date);
        user.setLocked(0);
        user.setOldid(member.getId().longValue());
        user.setOldMemberCode(member.getMemberid());
        user.setArea(member.getCensusregister());
        user.setLat((double) 0);
        user.setLng((double) 0);
        user.setClient(ConfigUtil.CLIENT_ID);

        return user;
    }
}

